package com.personal.portal.admin.controller;

import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.SkillCategory;
import com.personal.portal.admin.service.SkillCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 技能分类控制器
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Tag(name = "技能分类", description = "技能分类信息管理接口")
@RestController
@RequestMapping("/admin/skill-category")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class SkillCategoryController {

    @Resource
    private SkillCategoryService skillCategoryService;

    /**
     * 根据ID查询技能分类
     */
    @Operation(summary = "根据ID查询技能分类", description = "根据技能分类ID查询详细信息")
    @GetMapping("/{id}")
    public Result<SkillCategory> queryById(
            @Parameter(description = "技能分类ID", required = true) 
            @PathVariable Long id) {
        try {
            SkillCategory skillCategory = skillCategoryService.queryById(id);
            if (skillCategory != null) {
                return Result.success(skillCategory);
            } else {
                return Result.error("技能分类信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询技能分类信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据条件查询技能分类列表
     */
    @Operation(summary = "根据条件查询技能分类列表", description = "根据分类名称等条件查询技能分类信息")
    @PostMapping("/list")
    public Result<List<SkillCategory>> queryList(
            @Parameter(description = "技能分类查询条件") 
            @RequestBody SkillCategory skillCategory) {
        try {
            List<SkillCategory> skillCategoryList = skillCategoryService.queryList(skillCategory);
            return Result.success(skillCategoryList);
        } catch (Exception e) {
            return Result.error("查询技能分类列表失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询技能分类
     */
    @Operation(summary = "分页查询技能分类", description = "支持分页和条件查询的技能分类信息")
    @PostMapping("/page")
    public Result<Result.PageResult<SkillCategory>> queryPage(
            @Parameter(description = "页码，从1开始", example = "1") 
            @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") 
            @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "技能分类查询条件") 
            @RequestBody SkillCategory skillCategory) {
        try {
            Result.PageResult<SkillCategory> pageResult = skillCategoryService.queryPage(pageNum, pageSize, skillCategory);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("分页查询技能分类失败: " + e.getMessage());
        }
    }

    /**
     * 创建技能分类
     */
    @Operation(summary = "创建技能分类", description = "新增一条技能分类信息")
    @PostMapping
    public Result<Long> create(
            @Parameter(description = "技能分类信息", required = true) 
            @RequestBody SkillCategory skillCategory) {
        try {
            int result = skillCategoryService.insert(skillCategory);
            if (result > 0) {
                return Result.success("创建技能分类成功", skillCategory.getId());
            } else {
                return Result.error("创建技能分类失败");
            }
        } catch (Exception e) {
            return Result.error("创建技能分类失败: " + e.getMessage());
        }
    }

    /**
     * 更新技能分类
     */
    @Operation(summary = "更新技能分类", description = "更新指定ID的技能分类信息")
    @PutMapping("/{id}")
    public Result<Long> update(
            @Parameter(description = "技能分类ID", required = true) 
            @PathVariable Long id,
            @Parameter(description = "技能分类信息", required = true) 
            @RequestBody SkillCategory skillCategory) {
        try {
            skillCategory.setId(id);
            int result = skillCategoryService.update(skillCategory);
            if (result > 0) {
                return Result.success("更新技能分类成功", id);
            } else {
                return Result.error("更新技能分类失败");
            }
        } catch (Exception e) {
            return Result.error("更新技能分类失败: " + e.getMessage());
        }
    }

    /**
     * 删除技能分类
     */
    @Operation(summary = "删除技能分类", description = "根据ID删除指定的技能分类信息")
    @DeleteMapping("/{id}")
    public Result<Long> delete(
            @Parameter(description = "技能分类ID", required = true) 
            @PathVariable Long id) {
        try {
            int result = skillCategoryService.deleteById(id);
            if (result > 0) {
                return Result.success("删除技能分类成功", id);
            } else {
                return Result.error("删除技能分类失败");
            }
        } catch (Exception e) {
            return Result.error("删除技能分类失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除技能分类
     */
    @Operation(summary = "批量删除技能分类", description = "根据ID列表批量删除技能分类信息")
    @DeleteMapping("/batch")
    public Result<List<Long>> deleteBatch(
            @Parameter(description = "技能分类ID列表", required = true) 
            @RequestBody List<Long> ids) {
        try {
            List<Long> successIds = skillCategoryService.deleteBatch(ids);
            return Result.success(
                String.format("成功删除 %d 条记录", successIds.size()), successIds);
        } catch (Exception e) {
            return Result.error("批量删除技能分类失败: " + e.getMessage());
        }
    }
}